package pack

import (
	"mall_admin_api/biz/model"
	"mall_admin_api/biz/model/admin"
	"mall_admin_api/biz/model/admin_administrator"
	"mall_admin_api/biz/model/admin_menu"
)

func DealMenuList(list []*model.AdminMenu, isFirst bool) []*admin.Menu {
	//var childMap = make(map[int64][]*model.AdminMenu)
	//var rootList = make([]*model.AdminMenu, 0)
	var data = make([]*admin.Menu, 0)
	if isFirst {
		data = append(data, &admin.Menu{
			ID:   0,
			Name: "父级菜单",
		})
	}

	//for _, menu := range list {
	//	if menu.ParentID != 0 {
	//		childMap[menu.ParentID] = append(childMap[menu.ParentID], menu)
	//	} else {
	//		rootList = append(rootList, menu)
	//	}
	//
	//}
	//for _, menu := range rootList {
	//	newMenu := menu
	//	data = append(data, &admin.Menu{
	//		ID:   newMenu.ID,
	//		Name: newMenu.Name,
	//		PID:  newMenu.ParentID,
	//	})
	//	if d, ok := childMap[newMenu.ID]; ok {
	//		for _, adminMenu := range d {
	//			data = append(data, &admin.Menu{
	//				ID:   adminMenu.ID,
	//				Name: adminMenu.Name,
	//				PID:  adminMenu.ParentID,
	//			})
	//		}
	//
	//	}
	//
	//}
	return data
}

func GetAdministratorInfo(info *model.Administrator, permissions []string) *admin_administrator.Administrator {
	var data = admin_administrator.NewAdministrator()
	data.ID = info.ID
	data.Avatar = info.Icon
	data.Name = info.Nickname
	data.Permissions = permissions
	return data
}

func GetBIZList(list []*model.Biz) []*admin.BIZ {
	var data = make([]*admin.BIZ, 0)
	if len(list) == 0 {
		return data
	}
	for _, biz := range list {
		data = append(data, &admin.BIZ{
			ID:   biz.ID,
			Name: biz.Name,
			Key:  biz.Key,
		})
	}
	return data
}

func MenuBuild(list []*model.AdminMenu) []*admin_menu.AdminMenuBuild {
	return buildMenuBuild(buildTree(list))
}

func buildTree(list []*model.AdminMenu) []*model.AdminMenuWithChild {
	var data = make([]*model.AdminMenuWithChild, 0)
	for _, menu := range list {
		if menu.Pid == 0 {
			data = append(data, &model.AdminMenuWithChild{AdminMenu: menu})
		}
	}
	for key, menu := range data {
		var child = make([]*model.AdminMenuWithChild, 0)
		for _, adminMenu := range list {
			if adminMenu.Pid == menu.AdminMenu.ID {
				child = append(child, &model.AdminMenuWithChild{AdminMenu: adminMenu})
			}
		}
		data[key].Child = child
	}
	return data
}

func buildMenuBuild(list []*model.AdminMenuWithChild) []*admin_menu.AdminMenuBuild {
	var data = make([]*admin_menu.AdminMenuBuild, 0)
	for _, menu := range list {
		var temp = admin_menu.NewAdminMenuBuild()
		if menu.AdminMenu.ComponentName != "" {
			temp.Name = menu.AdminMenu.ComponentName
		} else {
			temp.Name = menu.AdminMenu.Name
		}
		if menu.AdminMenu.Pid == 0 {
			temp.Path = "/" + menu.AdminMenu.Path
		} else {
			temp.Path = menu.AdminMenu.Path
		}
		temp.Hidden = menu.AdminMenu.Hidden

		//判断不是外链
		if menu.AdminMenu.IFrame == 0 {
			if menu.AdminMenu.Pid == 0 {
				if menu.AdminMenu.Component == "" {
					temp.Component = "Layout"
				} else {
					temp.Component = menu.AdminMenu.Component
				}
			} else if menu.AdminMenu.Component != "" {
				temp.Component = menu.AdminMenu.Component
			}
		}

		temp.Meta = &admin_menu.AdminMenuBuildMeta{Title: menu.AdminMenu.Name, Icon: menu.AdminMenu.Icon, NoCache: menu.AdminMenu.Cache}
		if len(menu.Child) > 0 {
			temp.AlwaysShow = true
			temp.Redirect = "noredirect"
			temp.Children = buildMenuBuild(menu.Child)
		} else if menu.AdminMenu.Pid == 0 {
			var menuVo1 = admin_menu.NewAdminMenuBuild()
			menuVo1.Meta = temp.Meta
			if menu.AdminMenu.IFrame == 0 {
				menuVo1.Path = "index"
				menuVo1.Name = temp.Name
				menuVo1.Component = temp.Component
			} else {
				menuVo1.Path = menu.AdminMenu.Path
			}
			temp.Name = ""
			temp.Meta = admin_menu.NewAdminMenuBuildMeta()
			temp.Component = "Layout"
			var list1 []*admin_menu.AdminMenuBuild
			list1 = append(list1, menuVo1)
			temp.Children = list1
		}
		data = append(data, temp)
	}
	return data
}

func GetMenuList(list []*model.AdminMenu) []*admin_menu.AdminMenu {
	return buildMenuList(list, 0)
}

func buildMenuList(list []*model.AdminMenu, pid int64) []*admin_menu.AdminMenu {
	var data = make([]*admin_menu.AdminMenu, 0)
	for _, menu := range list {
		if menu.Pid == pid {
			var info = admin_menu.NewAdminMenu()
			info.ID = menu.ID
			info.Name = menu.Name
			info.Component = menu.Component
			info.PID = menu.Pid
			info.Sort = menu.Sort
			info.Hidden = menu.Hidden
			info.Permission = menu.Permission
			info.Type = menu.Type
			info.RouterMethod = menu.RouterMethod
			info.Router = menu.Router
			info.Icon = menu.Icon
			info.Path = menu.Path
			info.Label = menu.Name
			info.Children = buildMenuList(list, menu.ID)
			data = append(data, info)
		}
	}
	return data
}
